package com.example.taobaou.ui.fragment;


import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.taobaou.R;
import com.example.taobaou.base.BaseFragment;
import com.example.taobaou.model.domain.IBsaeInfo;
import com.example.taobaou.model.domain.SelectedContent;
import com.example.taobaou.model.domain.SelectedPage;
import com.example.taobaou.presenter.impl.SelectedPagePresenterImpl;
import com.example.taobaou.presenter.impl.TicketPresenterImpl;
import com.example.taobaou.ui.activity.TicketActivity;
import com.example.taobaou.ui.adapter.SelectedPageLiftAdapter;
import com.example.taobaou.ui.adapter.SelectedPageRightAdapter;
import com.example.taobaou.utils.LogUtils;
import com.example.taobaou.utils.PresenterManager;
import com.example.taobaou.utils.SizeUtils;
import com.example.taobaou.utils.TicketUtil;
import com.example.taobaou.view.ISelectedPageCallback;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SelectedFragment extends BaseFragment implements ISelectedPageCallback, SelectedPageLiftAdapter.onLeftItemClickListener, SelectedPageRightAdapter.OnSelectedPageContentItemClickListener {


    private SelectedPagePresenterImpl selectedPagePresenter;
    private RecyclerView leftRecyclerView;
    private RecyclerView rightRecyclerView;
    private SelectedPageLiftAdapter lift;
    private SelectedPageRightAdapter right;
    private TextView onSell;
    private TextView selected;

    public SelectedFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getRootViewResID() {
        return R.layout.fragment_selected;
    }

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_title_layout,container,false);
    }

    @Override
    protected void initView(View rootView) {
        setState(State.SUCCESS);
        lift = new SelectedPageLiftAdapter();
        leftRecyclerView = rootView.findViewById(R.id.left_recycleview);
        leftRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        leftRecyclerView.setAdapter(lift);

        right = new SelectedPageRightAdapter();
        rightRecyclerView = rootView.findViewById(R.id.right_recycleview);
        rightRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        rightRecyclerView.setAdapter(right);
        rightRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int topAndBotton = SizeUtils.dip2px(getContext(), 4);
                int leftAndRight = SizeUtils.dip2px(getContext(), 6);
                outRect.top = topAndBotton;
                outRect.bottom = topAndBotton;
                outRect.left = leftAndRight;
                outRect.right = leftAndRight;
            }
        });

        onSell = rootView.findViewById(R.id.on_sell_bar);
        onSell.setVisibility(View.GONE);
        selected = rootView.findViewById(R.id.selected_bar);
        selected.setVisibility(View.VISIBLE);

    }

    @Override
    protected void initPresenter() {
        super.initPresenter();
        LogUtils.d(this, "Presenter----------------------");
        selectedPagePresenter = PresenterManager.getInstance().getSelectedPagePresenter();
        selectedPagePresenter.registerViewCallback(this);
        selectedPagePresenter.getCategories();
    }

    @Override
    protected void release() {
        super.release();
        selectedPagePresenter.unregisiterViewCallback(this);
    }

    @Override
    protected void initListener() {

        lift.setOnLeftItemClickListener(this);
        right.setOnItemClickLisenter(this);
    }

    @Override
    public void onCategoriesLoaded(SelectedPage categories) {
        setState(State.SUCCESS);
        lift.setData(categories);
        //分类内容
        LogUtils.d(this, "onCategoriesLoaded" + categories);
//        List<SelectedPage.DataBean> data = categories.getData();
//        selectedPagePresenter.getContentByCategory(data.get(0));

    }

    @Override
    public void onContentLoad(SelectedContent content) {
        setState(State.SUCCESS);
        right.setData(content);
        rightRecyclerView.scrollToPosition(0);
        LogUtils.d(this, "右边——------------------------->" + content.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data());

    }


    @Override
    protected void onRetryClick() {
        if (selectedPagePresenter != null) {
            selectedPagePresenter.reloadContent();
        }
    }

    @Override
    public void onNatworkError() {
        setState(State.ERROR);
    }

    @Override
    public void onLoading() {
        setState(State.LOADING);

    }

    @Override
    public void onEmpty() {
        setState(State.EMPTY);
    }

    @Override
    public void leftItemClick(SelectedPage.DataBean item) {
        //左边的分类被点击了
        LogUtils.d(this,item.getFavorites_title());
        selectedPagePresenter.getContentByCategory(item);
    }

    @Override
    public void onItemClickListener(IBsaeInfo item) {
        //右边的内容被点击了
        //预先获取数据放进presenter，跳转activity后直接取出填入，防止加载慢
//        String title = item.getTitle();
//        String url = item.getCoupon_click_url();
//        if (TextUtils.isEmpty(url)){
//            url = item.getClick_url();
//        }
//        String cover = item.getPict_url();
//        TicketPresenterImpl ticketPresenter = PresenterManager.getInstance().getTicketPresenter();
//        ticketPresenter.getTicket(title,url,cover);
//        startActivity(new Intent(getContext(), TicketActivity.class));
        TicketUtil.toTicketPage(getContext(),item);
    }
}
